/*
 * @(#)RepositoryClient.java
 * Date 2013-04-11
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.repository.client;

import javax.enterprise.context.ApplicationScoped;

import javax.xml.ws.BindingProvider;

import javax.xml.ws.soap.AddressingFeature;

import ihe.iti.xds_b._2007.DocumentRepositoryPortType;
import ihe.iti.xds_b._2007.DocumentRepositoryService;
import ihe.iti.xds_b._2007.RetrieveDocumentSetRequestType;
import ihe.iti.xds_b._2007.RetrieveDocumentSetResponseType;

import us.hornerscorners.lamppost.config.EndpointConfig;

import us.hornerscorners.lamppost.model.audit.AuditContext;

import us.hornerscorners.lamppost.repository.service.AtnaRepositoryService;

import us.hornerscorners.lamppost.ws.AbstractWSClient;
import us.hornerscorners.lamppost.ws.AddressCallback;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-04-11
 * @author         Jim Horner
 */
@ApplicationScoped
public class RepositoryClient extends AbstractWSClient implements IRepositoryClient {

    /** Field description */
    private final AtnaRepositoryService atnaService;

    /** Field description */
    private final DocumentRepositoryService repositoryService;

    /**
     * Constructs ...
     *
     *
     * @param cfg
     */
    public RepositoryClient(AtnaRepositoryService atnaService, EndpointConfig cfg) {

        this(atnaService, new DocumentRepositoryService(), cfg);
    }

    /**
     * Constructs ...
     *
     *
     * @param repositoryService
     * @param cfg
     */
    public RepositoryClient(AtnaRepositoryService atnaService,
                            DocumentRepositoryService repositoryService,
                            EndpointConfig cfg) {

        super(cfg);
        this.repositoryService = repositoryService;
        this.atnaService = atnaService;
    }

    /**
     * Method description
     *
     *
     *
     * @param request
     * @return
     */
    @Override
    public RetrieveDocumentSetResponseType retrieveDocument(AuditContext auditContext,
            RetrieveDocumentSetRequestType request) {

        RetrieveDocumentSetResponseType result = null;

        DocumentRepositoryPortType port =
            this.repositoryService.getDocumentRepositoryPortSoap12(
                new AddressingFeature(true, true));

        AddressCallback addressCallback = configureBindingProvider(auditContext,
                                              (BindingProvider) port);

        result = port.documentRepositoryRetrieveDocumentSet(request);

        AuditContext outgoingContext = parseOutgoingContext(auditContext,
                                           addressCallback);

        this.atnaService.sendRetrieveRequestAuditMessage(outgoingContext, result);

        return result;
    }
}
